//
//  SCNRegisterViewController.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/5/17.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNRegisterViewController: SCNTableViewController {

    lazy var phoneTF: SCNLoginTextField = {
        let tf = SCNLoginTextField()
        tf.keyboardType = .numberPad
        tf.configuration(title: "+86", placeholder: "请输入您的手机号")
        return tf
    }()
    lazy var pswTF: SCNLoginTextField = {
        let tf = SCNLoginTextField()
        tf.keyboardType = .numberPad
        tf.configuration(title: "密 码", placeholder: "请输入最少6位数的密码")
        return tf
    }()
    lazy var securityCodeB: UIButton = {
        let btn = UIButton(type: .custom)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 13.5)
        btn.setTitle("发送验证码", for: .normal)
        btn.setTitleColor(SCNConstants.rootBackgroundColor, for: .normal)
        btn.addTarget(self, action: #selector(send(_:)), for: .touchUpInside)
        return btn

    }()
    lazy var securityCodeTF: SCNLoginTextField = {
        let tf = SCNLoginTextField()
        tf.keyboardType = .numberPad
        tf.configuration(title: "验证码", placeholder: "请输入短信验证码")
        return tf
    }()
    lazy var invitationCodeTF: SCNLoginTextField = {
        let tf = SCNLoginTextField()
        tf.keyboardType = .numberPad
        tf.configuration(title: "邀请码", placeholder: "请输入邀请码(选填)")
        return tf
    }()
    lazy var registerB: UIButton = {
        let btn = UIButton(type: .custom)
        btn.backgroundColor = SCNConstants.rootBackgroundColor
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16.0)
        btn.setTitle("立即注册", for: .normal)
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.layer.cornerRadius = 5.0
        btn.layer.masksToBounds = true
        btn.addTarget(self, action: #selector(register(_:)), for: .touchUpInside)
        return btn
    }()
    lazy var agreeL: UILabel = {
       
        let lab = UILabel()
        lab.text = "同意"
        lab.font = UIFont.systemFont(ofSize: 15.0)
        return lab
    }()
    lazy var delegateB: UIButton = {
        let btn = UIButton(type: .custom)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 15.0)
        btn.setTitle("守财奴用户协议", for: .normal)
        btn.setTitleColor(SCNConstants.rootBackgroundColor, for: .normal)
        btn.addTarget(self, action: #selector(SCNUserAgreement(_:)), for: .touchUpInside)
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "注册"
        
        view.addSubview(phoneTF)
        view.addSubview(pswTF)
        view.addSubview(securityCodeTF)
        view.addSubview(securityCodeB)
        view.addSubview(invitationCodeTF)
        view.addSubview(registerB)
        view.addSubview(agreeL)
        view.addSubview(delegateB)
        layout()
    }
    
    func layout() {
        phoneTF.snp.makeConstraints { (make) in
            make.top.equalTo(80)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: SCNConstants.SCREENWIDTH - 20,height: 40))
        }
        pswTF.snp.makeConstraints { (make) in
            make.top.equalTo(phoneTF.snp.bottom).offset(10)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: SCNConstants.SCREENWIDTH - 20,height: 40))
        }
        securityCodeB.snp.makeConstraints { (make) in
            
            make.top.equalTo(pswTF.snp.bottom).offset(10)
            make.trailing.equalTo(view.snp.trailing).offset(-15)
            make.size.equalTo(CGSize(width: 70,height: 40))
            
        }
        securityCodeTF.snp.makeConstraints { (make) in
            make.top.equalTo(pswTF.snp.bottom).offset(10)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: SCNConstants.SCREENWIDTH - 20,height: 40))
        }
        
        invitationCodeTF.snp.makeConstraints { (make) in
            make.top.equalTo(securityCodeTF.snp.bottom).offset(10)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: SCNConstants.SCREENWIDTH - 20,height: 40))
        }
        registerB.snp.makeConstraints { (make) in
            make.top.equalTo(invitationCodeTF.snp.bottom).offset(30)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: SCNConstants.SCREENWIDTH - 20,height: 40))
        }
        agreeL.snp.makeConstraints { (make) in
            make.leading.equalTo(registerB.snp.leading)
            make.top.equalTo(registerB.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 35,height: 20))
        }
        delegateB.snp.makeConstraints { (make) in
            make.leading.equalTo(agreeL.snp.trailing)
            make.top.equalTo(registerB.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 120,height: 20))
        }
    }

    func register(_ sender: UIButton) {
        if let tmpPhone = phoneTF.text,let tmpPsw = pswTF.text,let tmpSecurityCode = securityCodeTF.text,let tmpInvitationCode = invitationCodeTF.text {
            guard !tmpPhone.isEmpty else {
                self.noticeOnlyText("请输入您的手机号")
                return
            }
            guard !tmpPsw.isEmpty else {
                self.noticeOnlyText("请输入密码")
                return
            }
            guard !tmpSecurityCode.isEmpty else {
                self.noticeOnlyText("请输入验证码")
                return
            }
            let param = [
                "mobile": tmpPhone,
                "pwd": tmpPsw,
                "code": tmpSecurityCode,
                "invite":tmpInvitationCode,
                "device_token":String(UIDevice.current.systemName)
            ]
            SCNRequestManager.postRequest(param, URL: SCNRequestAPI.menber().register, completeBlock: { (result) in
                self.noticeOnlyText("恭喜🎉！注册成功")
                
                DispatchQueue.main.asyncAfter(deadline: .now() + 1.7) {
                    
                    self.navigationController?.popViewController(animated: true)
            
                }
                
            })
        }
    }
    
    func send(_ sender: UIButton) {
        self.sendSecurityCode(phoneTF.text)
    }

    func SCNUserAgreement(_ sender: UIButton) {
        
        self.loadWeb(at: self, url: "http://m.test.shoucainu8.com/company/agreement/pagesource/app", title: "守财奴用户协议")
    }
}
